import { defineStore } from 'pinia';
import { initMqtt } from '@/utils/mqtt.js'

export const useUserStore = defineStore('useUserStore', {
  state: () => {
    // @ts-ignore
    return {
      showMenu: false,
      equId: 0,
      equList: [],
      showVideo: false,
      mqtt: null,
      vehicleData: null,
      perceptionObject: [],
      searchValue: '',
      searchValueId: 0,
      showSearchRoadValue: false,
      layer1In: false,
      layer2In: false,
      layer3In: false,
      showLevel3: false,
      layerType: '',
      linkSearchShow: false,
      shijiaoShow: false,
      sjShow: false,
      showAlertModel: false,
      vehicleNum: '',
      globalType: 0,
    };
  },
  getters: {
    getShowMenu(state) {
      return state.showMenu;
    },
    getEquId(state) {
      return state.equId
    },
    getEquList(state) {
      return state.equList
    },
    getVehicleData(state) {
      return state.vehicleData
    },
    getPerceptionObject(state) {
      return state.perceptionObject
    },
    getSearchValue(state) {
      return state.searchValue
    },
    getSearchValueId(state) {
      return state.searchValueId
    },
    getShowSerchRoadValue(state) {
      return state.showSearchRoadValue
    },
    getShowLayer1In(state) {
      return state.layer1In
    },
    getShowLayer2In(state) {
      return state.layer2In
    },
    getShowLayer3In(state) {
      return state.layer2In
    },
    getShowLevel3(state) {
      return state.showLevel3
    },
    getLayerType(state) {
      return state.layerType
    },
    getLinkSearchShow(state) {
      return state.linkSearchShow
    },
    getShijiaoShow(state) {
      return state.shijiaoShow
    },
    getSJShow(state) {
      return state.sjShow
    },
    getShowAlertModel(state) {
      return state.showAlertModel
    },
    getVehicleNum(state) {
      return state.vehicleNum
    },
  },
  actions: {
    isShowMenu(menu) {
      this.showMenu = menu
    },
    setPerceptionObject(arr) {
      this.perceptionObject = arr
    },
    setEquId(id) {
      this.equId = id
    },
    setSearchValue(value) {
      this.searchValue = value
    },
    setSearchValueId(value) {
      this.searchValueId = value
    },
    setShowSearchRoadValue(value) {
      this.showSearchRoadValue = value
    },
    setShowLayer1In(value) {
      this.layer1In = value
    },
    setShowLayer2In(value) {
      this.layer2In = value
    },
    setShowLayer3In(value) {
      this.layer3In = value
    },
    setSowLevel3(value) {
      this.showLevel3 = value
    },
    setLayerType(type) {
      this.layerType = type
    },
    setLinkSearchShow(value) {
      this.linkSearchShow = value
    },
    setShijiaoShow(value) {
      this.shijiaoShow = value
    },
    setSJShow(value) {
      this.sjShow = value
    },
    setShowAlertModel(value) {
      this.showAlertModel = value
    },
    setVehicleNum(num) {
      this.vehicleNum = num
    },
    setGlobalType(type) {
      this.globalType = type
    },
    setEquList(data) {
      this.equList = data
      this.equId = this.equList[0].typeId
      for (let i in data) {
        if (data[i].typeId === '009') {
          this.equList[i].imageUrl = '[publicPath]static/images/icon_xinhaodeng_nor.png'
        }
        if (data[i].typeId === '003') {
          this.equList[i].imageUrl = '[publicPath]static/images/icon_shexiangji_nor.png'
        }
        if (data[i].typeId === '004') {
          this.equList[i].imageUrl = '[publicPath]static/images/icon_leida_nor.png'
        }
        if (data[i].typeId === '001') {
          this.equList[i].imageUrl = '[publicPath]static/images/icon_rsu_nor.png'
        }
        if (data[i].typeId === '002') {
          this.equList[i].imageUrl = '[publicPath]static/images/icon_mec_nor.png'
        }
      }
      if (this.equId === '009') {
        this.equList[0].imageUrl = '[publicPath]static/images/icon_xinhaodeng_sel.png'
      }
      if (this.equId === '003') {
        this.equList[0].imageUrl = '[publicPath]static/images/icon_shexiangji_sel.png'
      }
      if (this.equId === '004') {
        this.equList[0].imageUrl = '[publicPath]static/images/icon_leida_sel.png'
      }
      if (this.equId === '001') {
        this.equList[0].imageUrl = '[publicPath]static/images/icon_rsu_sel.png'
      }
      if (this.equId === '002') {
        this.equList[0].imageUrl = '[publicPath]static/images/icon_mec_sel.png'
      }
    },
    setShowVideo(data) {
      this.showVideo = data
    },
    initMqttVehicle(config) {
      this.mqtt = null
      this.mqtt = initMqtt(config)
    },
    setVehicleData(data) {
      this.vehicleData = data
    },
    closeMqttClient() {
      if (this.mqtt) {
        if (this.mqtt.uri) {
          if (this.mqtt.isConnected()) {
            console.log('断开mqtt连接')
            this.mqtt.disconnect()
            this.mqtt = null
            this.vehicleData = null
          }
        }
      }
    }
  }
});
